﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CUIT.GraduationProject.ToolBoxSystem.PropertyLib;
using CUIT.GraduationProject.ToolBoxSystem.ToolBoxLib;
using CUIT.GraduationProject.ToolBoxSystem.ControlData;
using CUIT.GraduationProject.ToolBoxSystem.ControlFactory;

namespace CUIT.GraduationProject.ToolBoxSystem.Platformd
{
    /// <summary>
    ///  平台
    /// </summary>
    public partial class MainPlatform : Form
    {
        private int count = 1;
        private ControlType newControl;
        public static ToolBox g_tb; // 工具箱
        public static System.Drawing.Rectangle g_FormClientRectangle; // 主界面窗体客户区
        public static PropertyLibUsersControl proForm;
        public Control tranControl;     // 记录修改属性的控件

        /// <summary>
        ///  加载
        /// </summary>
        public MainPlatform()
        {
            InitializeComponent();
            InitializeToolBoxLib();
            ////InitializePropertyLib(); // 初始化属性窗口

            DragDrop += MainPlatform_DragDrop;
            DragEnter += MainPlatform_DragEnter;
            Load += MainPlatform_Load;

            g_tb = new ToolBox(new Size(200, ClientRectangle.Height - 0), 22, null);
            g_tb.Location = new Point(0, 50);

            //g_tb.SetClickDelegate(new ToolBox.OnToolBoxClick(OnTBClick));
            //g_tb.SetTabChangedDelegate(new ToolBox.OnToolBoxTabChanged(OnTBTabChanged));
            //g_tb.SetStateChangedDelegate(new ToolBox.OnToolBoxStateChanged(OnTBStateChanged));
            g_tb.Show();
            this.Controls.Add(g_tb);
            g_tb.BringToFront();
        }

        private void MainPlatform_Load(object sender, EventArgs e)
        {
            this.AllowDrop = true;
            //窗体自身支持接受拖拽来的控件
        }

        private void MainPlatform_DragEnter(object sender, DragEventArgs e)
        {
            //当Button被拖拽到WinForm上时候，鼠标效果出现
            if ((e.Data.GetDataPresent(typeof(Button))))
            {
                newControl = ControlType.Button;
            }
            else if (e.Data.GetDataPresent(typeof(TextBox)))
            {
                newControl = ControlType.TextBox;
            }

            e.Effect = DragDropEffects.Copy;
        }

        private void MainPlatform_DragDrop(object sender, DragEventArgs e)
        {
            //拖放完毕之后，自动生成新控件
            Control btn = CreateControlFactory.NewControl(newControl);
            btn.Size = new Size(50, 50);
            btn.Location = this.PointToClient(new Point(e.X, e.Y));
            //用这个方法计算出客户端容器界面的X，Y坐标。否则直接使用X，Y是屏幕坐标
            this.Controls.Add(btn);
            btn.Text = newControl.ToString() + count.ToString();

            ContextMenuStrip ms = new ContextMenuStrip();
            ms.Items.Add("全选(Ctrl+A)");
            ms.Items.Add("复制(Ctrl+C)");
            ms.Items.Add("粘贴(Ctrl+V)");
            ms.Items.Add("删除(Delete)");
            ms.Items.Add("属性");

            ms.ItemClicked += new ToolStripItemClickedEventHandler(ms_ItemClicked);
            btn.ContextMenuStrip = ms;
            count = count + 1;
        }

        /// <summary>
        ///  右击快捷键菜单列表项点击事件
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        void ms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            TextBox tb = ((TextBox)((ContextMenuStrip)sender).SourceControl);
            if (((ContextMenuStrip)sender).Items[0] == e.ClickedItem)
            {
                tb.SelectAll();
            }
            else if (((ContextMenuStrip)sender).Items[1] == e.ClickedItem)
            {
                Clipboard.SetDataObject(tb.SelectedText);
            }
            else if (((ContextMenuStrip)sender).Items[2] == e.ClickedItem)
            {
                IDataObject iData = Clipboard.GetDataObject();
                if (iData.GetDataPresent(DataFormats.Text))
                {
                    tb.Text += (String)iData.GetData(DataFormats.Text);
                }
            }
            else if (((ContextMenuStrip)sender).Items[3] == e.ClickedItem)
            {
                tb.Text = "";
            }
            else if (((ContextMenuStrip)sender).Items[4] == e.ClickedItem)
            {
                tranControl = tb;
                // 打开属性面板  属性加载进去
                InitializePropertyLib(tb);
            }
        }

        /// <summary>
        ///  初始化工具箱
        /// </summary>
        private void InitializeToolBoxLib()
        {
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.SizeChanged += new System.EventHandler(this.MainPlatform_SizeChanged);
        }

        /// <summary>
        ///  初始化属性窗口
        /// </summary>
        private void InitializePropertyLib(Control ctl)
        {
            proForm = new PropertyLibUsersControl();
            proForm.Size = new Size(200, ClientRectangle.Height - 0);
            proForm.Location = new Point(1165, 50);

            if (ctl != null)
            {
                proForm.Ctr = new ControlProperty();
                proForm.Ctr.Name = ctl.Name;
                proForm.Ctr.Location = ctl.Location;
                proForm.Ctr.Size = ctl.Size;
                proForm.Ctr.Visible = true;
                proForm.PropertyKeyDownEnter += new PropertyLibUsersControl.KeyDownEnty(InitialTranControl);
            }

            proForm.Show();
            this.Controls.Add(proForm);
            proForm.BringToFront();
        }

       /// <summary>
       ///  初始化修改属性后的控件
       /// </summary>
        /// <param name="pro">属性</param>
        private void InitialTranControl(ControlProperty pro)
        {
            tranControl.Name = pro.Name;
            tranControl.Location = pro.Location;
            tranControl.Size = pro.Size;
            tranControl.Visible = pro.Visible;
        }

        /// <summary>
        ///  窗体大小改变事件
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void MainPlatform_SizeChanged(object sender, EventArgs e)
        {
            g_tb.SetSize(new Size(g_tb.Width, ClientRectangle.Height - 0));
            g_FormClientRectangle = this.ClientRectangle;
            ////proForm.SetSize(new Size(proForm.Width, ClientRectangle.Height - 0));
        }

        /// <summary>
        ///  打开属性
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Tls_Property_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  剪切
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">额</param>
        private void Tls_Cut_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  复制
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Tls_Copy_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  粘贴
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Tls_Paste_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  撤销
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Tls_Repeal_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  保存
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Tls_Save_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  打开
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Open_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  保存
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Save_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  退出
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        ///  复制
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Copy_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  粘贴
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Paste_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  关于
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void About_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  基本设置
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void CommonSettings_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  收展设置
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void RollSettings_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 抽屉设置
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void ItemSettings_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  展开
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void UpSettings_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  打开工具箱
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void OpenToolBox_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 剪切
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Cut_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  删除
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void Delete_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  撤销
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void RollBack_Click(object sender, EventArgs e)
        {

        }
    }
}
